package com.dotflat.docs.service.impl;

import com.dotflat.docs.domain.Document;
import com.dotflat.docs.domain.DocumentDirectory;
import com.dotflat.docs.po.DocumentPO;
import com.dotflat.docs.repository.DocumentRepository;
import com.dotflat.docs.service.DocumentDirectoryService;
import com.dotflat.docs.service.DocumentService;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
//import com.starinside.wp.foundation.exception.BusinessException;
//import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DocumentServiceImpl extends AreableObjectBaseServiceImpl<Document, DocumentRepository> implements DocumentService {

    @Autowired
    @Override
    public void setBaseRepository(DocumentRepository repository){
        super.setBaseRepository(repository);
    }

    @Autowired
    private DocumentDirectoryService directoryService;

    @Override
    public Document add(String areaId, String staffId, AreableObjectBasePO param) {
        if (!(param instanceof DocumentPO)){
            return super.add(areaId, staffId, param);
        }
        DocumentPO po = ((DocumentPO)param);
        String dirId = po.getDirId();

        DocumentDirectory dir = directoryService.get(areaId, staffId, dirId);

        if (dir == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "目录不存在");
        }

        return add(areaId, staffId, param, o->{
            o.setDirTitle(dir.getTitle());
            o.setDirType(dir.getType());
            o.setStaffName(((DocumentPO) param).getStaffName());
        });
    }

}